﻿using Common.Enums;
using FreeSql.Context;
using FreeSql.Model;
using Microsoft.AspNetCore.Mvc;
using System.Linq;
using System.Threading.Tasks;

namespace enterprise.Website.Core.Controllers
{
    public class HomeController : Controller
    {
        private readonly SqlContext _sqlContext;
        private readonly BaseWebElement _baseWebElement;
        public HomeController(SqlContext sqlContext, BaseWebElement baseWebElement)
        {
            _sqlContext = sqlContext;
            _baseWebElement = baseWebElement;
        }

        /// <summary>
        /// 首页
        /// </summary>
        /// <returns></returns>
        [Route("{languageClass:maxlength(2)?}")]
        public IActionResult Index()
        {
            return View();
        }

        /// <summary>
        /// 内容页
        /// </summary>
        /// <param name="path"></param>
        /// <returns></returns>
        [Route(template:"{languageClass:maxlength(2)}/{path:minlength(3)}",Order = 200)]
        [Route(template:"{path:minlength(3)}",Order =190)]
        public IActionResult contentView(string path)
        {
            ViewData["path"] = path;
            var Model = _baseWebElement.GetContentClass().Where(m => m.Path == path).OrderByDescending(m => m.Sort).FirstOrDefault();
            if (Model == null)
            {
                return NotFound();
            }
            return View(Model);
        }

        /// <summary>
        /// 产品列表页
        /// </summary>
        /// <returns></returns>
        [Route(template: "products", Order = 181)]
        [Route(template: "{languageClass:maxlength(2)}/products", Order = 182)]
        [Route(template:"products/catelog/{cid}.html",Order = 170)]
        [Route(template: "{languageClass:maxlength(2)}/products/catelog/{cid}.html", Order = 180)]
        public IActionResult Products(int cid = 0, int page = 1,string searchText = "")
        {
            ViewData["classId"] = cid;
            ViewData["page"] = page;
            ViewData["searchText"] = searchText;
            var Model = _baseWebElement.GetContentClass().Where(cid == 0 ? (m=>m.ClassType == (int)ClassType.Products && m.ParentId == 0) :(m=>m.Id==cid)).OrderByDescending(m => m.Sort).FirstOrDefault();
            if (Model == null)
            {
                return NotFound();
            }
            return View(Model);
        }

        /// <summary>
        /// 产品详情页
        /// </summary>
        /// <param name="pid"></param>
        /// <returns></returns>
        [Route(template: "products/detail/{pid}.html", Order = 150)]
        [Route(template: "{languageClass:maxlength(2)}/products/detail/{pid}.html", Order = 160)]

        public async Task<IActionResult> ProductDetails(int pid)
        {
            ViewData["pid"] = pid;
            var ProductInfo = await _sqlContext.QueryInfoAsync<Product_Info>(m => m.Id == pid&&m.LanguageId==_baseWebElement.Currentlanguage.Id);
            if (ProductInfo == null)
            {
                return Redirect(_baseWebElement.GetCurrentlanguageclassHref() + "products");
            }
            return View(ProductInfo);
        }

        /// <summary>
        /// 询盘页
        /// </summary>
        /// <param name="pid"></param>
        /// <returns></returns>
        [Route(template: "inquiry/{pid}.html", Order = 130)]
        [Route(template: "{languageClass:maxlength(2)}/inquiry/{pid}.html", Order = 140)]
        public async Task<IActionResult> Inquiry(int pid)
        {
            ViewData["pid"] = pid;
            var ProductInfo = await _sqlContext.QueryInfoAsync<Product_Info>(m => m.Id == pid && m.LanguageId == _baseWebElement.Currentlanguage.Id);
            if (ProductInfo == null)
            {
                return Redirect(_baseWebElement.GetCurrentlanguageclassHref() + "products");
            }
            return View(ProductInfo);
        }

        /// <summary>
        /// 联系我们页（附带提交功能，如果需要使用此页，请将联系我们页的Path配置为contact）
        /// </summary>
        /// <returns></returns>
        [Route(template: "contact", Order = 50)]
        [Route(template: "{languageClass:maxlength(2)}/contact", Order = 60)]
        public IActionResult contact()
        {
            var Model = _baseWebElement.GetContentClass().Where(m => m.Path == "contact").OrderByDescending(m => m.Sort).FirstOrDefault();
            if (Model == null)
            {
                return NotFound();
            }
            return View(Model);
        }

        /// <summary>
        /// 新闻列表页
        /// </summary>
        /// <returns></returns>
        [Route(template: "news", Order = 31)]
        [Route(template: "{languageClass:maxlength(2)}/news", Order = 32)]
        [Route(template: "news/catelog/{cid}.html", Order = 30)]
        [Route(template: "{languageClass:maxlength(2)}/news/catelog/{cid}.html", Order = 40)]
        public IActionResult News(int cid = 0,int page =1)
        {
            ViewData["classId"] = cid;
            ViewData["page"] = page;
            var ClassInfo = _baseWebElement.GetContentClass().Where(cid == 0 ? (m => m.ClassType == (int)ClassType.News && m.ParentId == 0) : (m => m.Id == cid)).OrderByDescending(m => m.Sort).FirstOrDefault();
            if (ClassInfo == null)
            {
                return NotFound();
            }
            return View(ClassInfo);
        }

        /// <summary>
        /// 新闻详情页
        /// </summary>
        /// <param name="newsId"></param>
        /// <returns></returns>
        [Route(template: "news/detail/{newsId}.html", Order = 10)]
        [Route(template: "{languageClass:maxlength(2)}/news/detail/{newsId}.html", Order = 20)]
        public async Task<IActionResult> NewsDetail(int newsId)
        {
            var NewsInfo = await _sqlContext.QueryInfoAsync<News_Info>(m => m.Id == newsId && m.LanguageId == _baseWebElement.Currentlanguage.Id);
            if (NewsInfo == null)
            {
                return Redirect(_baseWebElement.GetCurrentlanguageclassHref() + "news");
            }
            return View(NewsInfo);
        }
    }
}
